package com.app.qbake.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.wind.base.dialog.AlertDialogUtil;
import com.app.qbake.R;
import com.wind.toastlib.ToastUtil;


/**
 * Created by wind on 2017/6/12.
 */

public class AppDialogHelper {



  /*  public static void showItemsDialogWithOneBtn(Context context,String title,String btnName,String []items,final DialogOperCallback callback){
        final AlertDialog alertDialog = AlertDialogUtil.showAlertDialog(context, R.layout.dialog_items_with_onebtn, false);
        TextView tv_dialog_title= (TextView) alertDialog.findViewById(R.id.tv_dialog_title);
        tv_dialog_title.setText(title);

        RecyclerView rv= (RecyclerView) alertDialog.findViewById(R.id.rv);
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(manager);
        //String items[]=context.getResources().getStringArray(arrayRes);
        DialogStringItemAdapter adapter=new DialogStringItemAdapter(context,items);
        rv.setAdapter(adapter);

        TextView tv_btn= (TextView) alertDialog.findViewById(R.id.tv_btn);
        tv_btn.setText(btnName);
        tv_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                callback.onDialogConfirmClick();
            }
        });
    }

    public static void showItemsDialogWithBtns(Context context,String title,String subTitle,String btnName,String []items,final DialogOperCallback callback) {
        final AlertDialog alertDialog = AlertDialogUtil.showAlertDialog(context, R.layout.dialog_items_with_btns, false);
        TextView tv_dialog_title= (TextView) alertDialog.findViewById(R.id.tv_dialog_title);
        tv_dialog_title.setText(title);
        TextView tv_dialog_subtitle= (TextView) alertDialog.findViewById(R.id.tv_dialog_subtitle);
        tv_dialog_subtitle.setText(subTitle);
        RecyclerView rv= (RecyclerView) alertDialog.findViewById(R.id.rv);
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(manager);
        //String items[]=context.getResources().getStringArray(arrayRes);
        DialogStringItemAdapter adapter=new DialogStringItemAdapter(context,items);
        rv.setAdapter(adapter);

        TextView tv_btn= (TextView) alertDialog.findViewById(R.id.tv_btn);
        tv_btn.setText(btnName);
        tv_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                callback.onDialogConfirmClick();
            }
        });
        TextView tv_close= (TextView) alertDialog.findViewById(R.id.tv_close);
        tv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                callback.onDialogCancelClick();
            }
        });
        tv_close.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );
        alertDialog.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
    }
  */
  public static void showTipDialog(Context context,String msg,String leftBtnName,String rightBtnName,final DialogOperCallback callback){
      final AlertDialog alertDialog = AlertDialogUtil.showAlertDialog(context, R.layout.dialog_tip, false,false);
      TextView tv_left_btn=(TextView)alertDialog.findViewById(R.id.tv_left_btn);
      TextView tv_right_btn=(TextView)alertDialog.findViewById(R.id.tv_right_btn);
      TextView tv_msg=(TextView)alertDialog.findViewById(R.id.tv_msg);
      tv_msg.setText(msg);
      tv_left_btn.setText(leftBtnName);
      tv_right_btn.setText(rightBtnName);
      alertDialog.findViewById(R.id.tv_left_btn).setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              alertDialog.dismiss();
              callback.onDialogCancelClick();
          }
      });
      alertDialog.findViewById(R.id.tv_right_btn).setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              alertDialog.dismiss();
              callback.onDialogConfirmClick();
          }
      });
  }
    public static void showNormalDialog(Context context,String msg,String leftBtnName,String rightBtnName,final DialogOperCallback callback){
        final AlertDialog alertDialog = AlertDialogUtil.showAlertDialog(context, R.layout.dialog_normal, false,false);
        TextView tv_left_btn=(TextView)alertDialog.findViewById(R.id.tv_left_btn);
        TextView tv_right_btn=(TextView)alertDialog.findViewById(R.id.tv_right_btn);
        TextView tv_msg=(TextView)alertDialog.findViewById(R.id.tv_msg);
        tv_msg.setText(msg);
        tv_left_btn.setText(leftBtnName);
        tv_right_btn.setText(rightBtnName);
        alertDialog.findViewById(R.id.tv_left_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                callback.onDialogCancelClick();
            }
        });
        alertDialog.findViewById(R.id.tv_right_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                callback.onDialogConfirmClick();
            }
        });
    }

   /* public static void showNormalSingleBtnDialog(Context context,String msg,String btnName,final DialogOperCallback callback){
        final AlertDialog alertDialog = AlertDialogUtil.showAlertDialog(context, R.layout.dialog_singlebtn_normal, false);
        TextView tv_btn=(TextView)alertDialog.findViewById(R.id.tv_btn);
        TextView tv_msg=(TextView)alertDialog.findViewById(R.id.tv_msg);
        tv_msg.setText(msg);
        tv_btn.setText(btnName);
        alertDialog.findViewById(R.id.tv_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                callback.onDialogConfirmClick();
            }
        });

    }*/
    public static void showNormalDialog(Context context,String msg,final DialogOperCallback callback){
        showNormalDialog(context,msg,"取消","确定",callback);
    }










    /*public static void showCoverOpraDialog(Context context,final DialogItemsCallback callback){
        final AlertDialog alertDialog = AlertDialogUtil.showAlertDialog(context, R.layout.dialog_cover_oper, true, true);

        alertDialog.findViewById(R.id.tv_op1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                callback.onDialogItemClick(0);
            }
        });
        alertDialog.findViewById(R.id.tv_op2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                callback.onDialogItemClick(1);
            }
        });

        alertDialog.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
    }*/

    /**
     * 头像选择弹窗
     *
     * @param context
     */
    public static void showAvatarSelectDialog(Context context, final DialogPhotoSelectCallback callback) {
        showPhotoSelectDialog(context,true,callback);
    }
    /**
     * 图片选择弹窗
     *
     * @param context
     */
    public static void showPhotoSelectDialog(Context context,boolean isAvatar ,final DialogPhotoSelectCallback callback) {
        final AlertDialog alertDialog = AlertDialogUtil.showAlertDialog(context, R.layout.dialog_camera_or_pic_layout, true, true);
       /* View view=alertDialog.findViewById(R.id.ll_avatar_header);
        if (isAvatar){
            view.setVisibility(View.VISIBLE);
        }else {
            view.setVisibility(View.GONE);
        }*/
        alertDialog.findViewById(R.id.tv_camera).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                callback.onDialogCameraClick();
            }
        });
        alertDialog.findViewById(R.id.tv_album).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                callback.onDialogAlbumClick();
            }
        });

        alertDialog.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
    }

    public static void showEditTextDialog(final Context context, int lines,String text, final DialogInputCallback callback) {

        final AlertDialog alertDialog = AlertDialogUtil.showAlertDialog(context, R.layout.dialog_editname, false,false,true);
        TextView tv_left_btn=(TextView)alertDialog.findViewById(R.id.tv_left_btn);
        TextView tv_right_btn=(TextView)alertDialog.findViewById(R.id.tv_right_btn);
        final EditText et_content=alertDialog.findViewById(R.id.et_content);
        et_content.setLines(lines);
        et_content.setText(text);
        alertDialog.findViewById(R.id.tv_left_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();

            }
        });
        alertDialog.findViewById(R.id.tv_right_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                String input=et_content.getText().toString().trim();
                if (TextUtils.isEmpty(input)){
                    ToastUtil.showToast((Activity) context,"请输入自我介绍");
                }else{
                    callback.onDialogInputReturn(input);
                }

            }
        });
    }

   /* public static void showItemsDialog(Activity activity, List<String> ops,final DialogItemsCallback callback) {
        final AlertDialog alertDialog = AlertDialogUtil.showAlertDialog(activity, R.layout.dialog_items, true,true);
        RecyclerView rv_items =alertDialog.findViewById(R.id.rv_items);
        alertDialog.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        LinearLayoutManager manager = new LinearLayoutManager(activity);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rv_items.setLayoutManager(manager);
        StringItemAdapter adapter = new StringItemAdapter(activity, ops, Gravity.LEFT);
        adapter.setOnItemClickListener(new StringItemAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                alertDialog.dismiss();
                callback.onDialogItemClick(position);
            }
        });
        rv_items.setAdapter(adapter);


    }*/

   /* public static void showPaymentDialog(Context context, String price, final PaymentDialogCallback callback) {
        final AlertDialog alertDialog = AlertDialogUtil.showAlertDialog(context, R.layout.dialog_payment, true, false);
        TextView tv_price=alertDialog.findViewById(R.id.tv_price);
        tv_price.setText(price);
        final View iv_alipay=alertDialog.findViewById(R.id.iv_alipay);
        iv_alipay.setActivated(true);
        final View iv_weixinpay=alertDialog.findViewById(R.id.iv_weixinpay);
        iv_alipay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_alipay.setActivated(true);
                iv_weixinpay.setActivated(false);
            }
        });
        iv_weixinpay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_alipay.setActivated(false);
                iv_weixinpay.setActivated(true);
            }
        });

        View tv_left_btn=alertDialog.findViewById(R.id.tv_left_btn);
        tv_left_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        View tv_right_btn=alertDialog.findViewById(R.id.tv_right_btn);
        tv_right_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PaymentChannel paymentChannel=PaymentChannel.ALIPAY;
                if (iv_weixinpay.isActivated()){
                    paymentChannel=PaymentChannel.WECHAT;
                }
                alertDialog.dismiss();
                callback.onPaymentSelected(paymentChannel);
            }
        });
    }
    public interface PaymentDialogCallback{
        void onPaymentSelected(PaymentChannel paymentChannel);
    }*/

    /**
     * 照片选择对话框 回调
     */
    public interface DialogPhotoSelectCallback {
        void onDialogCameraClick();

        void onDialogAlbumClick();
    }

    public interface DialogItemsCallback{
        void onDialogItemClick(int position);
    }
    /**
     * 对话框取消，确定按钮回调
     */
    public static abstract class DialogOperCallback {
        public  void onDialogCancelClick(){}
        public void onDialogNeutralClcik(){};
        public abstract void onDialogConfirmClick();
    }

    public static interface DialogInputCallback{
        void onDialogInputReturn(String input);
    }

}
